﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace WfTimedExecService
{
    public partial class ExceService : ServiceBase
    {
        public ExceService()
        {
            InitializeComponent();
        }

        System.Timers.Timer timer;
        Url_Request _Request;

        #region 服务启动
        protected override void OnStart(string[] args)
        {
            try
            {
                if (timer == null)
                    timer = new System.Timers.Timer();
                //设置时间
                int second = Read_Config.Config_ExecTime() * 1000;

                timer.Interval = second;

                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

                timer.Start();

                Service_Log.Log("服务启动", "");
            }
            catch (Exception ex)
            {
                Service_Log.Log("服务启动异常", ex.Message);
            }
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                string Url = Read_Config.Config_RequestUrl() + "?id=" + Time_Stamp.Timestamp();
                if (_Request == null)
                    _Request = new Url_Request();

                string Result = _Request.UrlRequest(Url);

                Service_Log.Log("服务正常执行", Result.ToString());
            }
            catch (Exception ex)
            {
                Service_Log.Log("服务异常执行", ex.Message);
            }
        }


        #endregion

        #region 服务停止
        protected override void OnStop()
        {
            try
            {
                timer.Stop();

                Service_Log.Log("服务正常停止", "");
            }
            catch (Exception ex)
            {
                Service_Log.Log("服务异常停止", ex.Message);
            }
        }
        #endregion
    }
}
